<?php

namespace App\Service;

use Closure;
use Duxravel\Core\UI\Table;
use Duxravel\Core\UI\Widget\Link;
use Duxravel\Core\UI\Widget\Widget;

/**
 *
 */
class TableService extends Table
{
    /**
     * @var Table|null
     */
    private static ?Table $table = null;

    public bool $showAdd = true;

    /**
     * @param $data
     * @param Closure $callback
     * @return Table|TableService|null
     */
    public static function make($data, Closure $callback){
        if(!(self::$table instanceof self)){
            self::$table = new self($data);
        }
        call_user_func($callback, self::$table);
        return self::$table;
    }

    /**
     * @param $route
     * @param $name
     * @param string $type
     * @param string $icon
     * @return Link
     */
    public function showAction($route, $name, string $type='default', string $icon = ''): Link
    {
        return self::$table->action()->button($name, $route)->type($type)->icon($icon);
    }

    /**
     * @param string $name
     * @param array|null $options
     * @param Closure|null $callback
     */
    public function showActions(string $name = '操作', array $options = [], Closure $callback = null){
        $column = self::$table->column($name);
        if($options){
            foreach ($options as $value){
                $column->link($value['name'], $value['route'], $value['params'])->type($value['type'])->icon('edit');
            }


            $column->link('删除', 'admin.wechat.setting.del', ['id' => 'id'])
                ->type('ajax', ['method' => 'post'])
                ->icon('delete')
                ->class("arco-link-status-danger")->attr('vIf','rowData.record.id!=1');
        }

    }

    /**
     * @param string $name
     * @param Closure|null $callback
     */
    protected function showEditAction($column,string $name = '', string $route = '',$params){
        $column->link($name, $route, $params)->type('dialog')->icon('edit');
    }

    /**
     * @param string $name
     * @param Closure|null $callback
     */
    protected function showColumnAction(string $name = '编辑', $column){
        $column->link($name, 'admin.wechat.setting.page', ['id' => 'id'])->type('dialog')->icon('edit');
        $column->link('删除', 'admin.wechat.setting.del', ['id' => 'id'])
            ->type('ajax', ['method' => 'post'])
            ->icon('delete')
            ->class("arco-link-status-danger")->attr('vIf','rowData.record.id!=1');
    }

    /**
     * 显示单图
     * @param string $name
     * @param string $field
     * @param int|string $width
     * @param int|string $height
     * @return Table\Column
     */
    public function showImage(string $name, string $field, $width = 40, $height = 40): Table\Column
    {
        return self::$table->column($name,$field)->node([
            'nodeName'=>'a-image',
            'vBind:src'=>'rowData.record.'.$field,
            'width'=> $width,
            'height'=> $height
        ]);
    }

}
